Find the Number of Vowels, Consonants, Digits and White Spaces in a String using C++

04-11-17 Course- CPP

In this program takes a string from user and finds the total number of vowels, consonants, digits and white spaces present in that string.

Source Code to Find Number of Vowels, Consonants, Digits and White Spaces in a String


#include<iostream>
#include<cstring>
using namespace std;
int main(){
    char line[150];
    int i,v,c,ch,d,s,o;
    o=v=c=ch=d=s=0;
    cout << "Enter a line of string: " << endl;
    cin.getline(line, 150);
    for(i=0;line[i]!='\0';++i)
    {
        if(line[i]=='a' 
           || line[i]=='e' 
           || line[i]=='i' 
           || line[i]=='o' 
           || line[i]=='u' 
           || line[i]=='A' 
           || line[i]=='E' 
           || line[i]=='I' 
           || line[i]=='O' 
           || line[i]=='U')
            ++v;
        else if((line[i]>='a'&& line[i]<='z') 
                 || (line[i]>='A'&& line[i]<='Z'))
            ++c;
        else if(line[i]>='0'&&c<='9')
            ++d;
        else if (line[i]==' ')
            ++s;
    }
    cout << " Vowels: " << v << endl;
    cout << " Consonants: " << c << endl;
    cout << " Digits: " << d << endl;    
    cout << " White Spaces: " << d << endl;    
    return 0;
}

Output


Enter a line of string:
programming is fun 1 234
Vowels: 5
Consonants: 11
Digits: 4
White Spaces: 4